package com.middlegroup.subjectpay.web.api;

import com.alibaba.fastjson.JSONObject;
import com.middlegroup.common.model.ResponseEntity;
import com.middlegroup.common.model.TransResult;
import com.middlegroup.subjectpay.dto.GetConfigDto;
import com.middlegroup.subjectpay.entity.SubjectChannelConfig;
import com.middlegroup.subjectpay.service.SubjectChannelConfigService;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 *配置接口
 * @author pc
 * @date 2019/1/24
 */
@RestController
public class ConfigController {

    @Resource
     private SubjectChannelConfigService subjectChannelConfigService;
    /**获取配置
     * @param getConfigDto
     * @return
     */
    @RequestMapping("getSubjectConfig")
    public ResponseEntity getConfigs(SubjectChannelConfig getConfigDto){

        List<SubjectChannelConfig> list = subjectChannelConfigService.getConfigs(getConfigDto);
        TransResult transResult = new TransResult();
        transResult.setData(list);
        return new ResponseEntity(ResponseEntity.STATUS_OK,transResult);
    }

    /**
     * @param configs
     * @return
     */
    @RequestMapping("putSubjectConfigs")
    public ResponseEntity putConfig(List<SubjectChannelConfig> configs){
        TransResult transResult = new TransResult();
        transResult.setTransStatus(0);
        subjectChannelConfigService.putConfigs(configs);
        transResult.setTransStatus(1);
        return new ResponseEntity(ResponseEntity.STATUS_OK,transResult);
    }

    /**
     * @param configs
     * @return
     */
    @RequestMapping("removeSubjectConfigs")
    public ResponseEntity removeConfigs(List<SubjectChannelConfig> configs){
        if (!CollectionUtils.isEmpty(configs)){
            for (SubjectChannelConfig subjectChannelConfig : configs){
                subjectChannelConfigService.removeConfig(subjectChannelConfig.getCategory(),subjectChannelConfig.getName());
            }
        }
        TransResult transResult = new TransResult();
        transResult.setTransStatus(1);
        return new ResponseEntity(ResponseEntity.STATUS_OK,transResult);
    }
}
